
//
//  WayPointCollectionViewCell.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-4-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WayPointCollectionViewCell.h"
#import "UIColor+AddColor.h"
@implementation WayPointCollectionViewCell
- (void)dealloc
{
    Block_release(_block);
    [_touchImageView release];
    [_wayLable release];
    [_footVImagev release];
    [_footHImagev release];
    [_clockImagev release];
    [_timeLable release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
        
        self.touchImageView = [[TouchImageView alloc]init];
        //        _wayImageV.contentMode = UIViewContentModeScaleAspectFit;
        _touchImageView.clipsToBounds = YES;
        _touchImageView.userInteractionEnabled = YES;
        _touchImageView.target = self;
        _touchImageView.action = @selector(imageTouchAction:);
        [self.contentView addSubview:_touchImageView];
        
        self.wayLable = [[UILabel alloc]init];
        [_wayLable sizeToFit];
        _wayLable.numberOfLines = 0;
        //        _wayLable.backgroundColor =[UIColor redColor];
        _wayLable.font = [UIFont fontWithName:@"Georgia" size:12];
        [self.contentView addSubview:_wayLable];
        
        self.footVImagev = [[UIImageView alloc]init];
        self.footVImagev.image = [UIImage imageNamed:@"qianlansekuai.png"];
        [self.contentView addSubview:_footVImagev];
        
        self.footHImagev = [[UIImageView alloc]init];
        self.footHImagev.image = [UIImage imageNamed: @"huiseyingyin.png"];
        [self.contentView addSubview:_footHImagev];
        
        self.clockImagev = [[UIImageView alloc]init];
        self.clockImagev.image = [UIImage imageNamed:@"clock.png"];
        self.clockImagev.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_clockImagev];
        
        self.timeLable = [[UILabel alloc]init];
        _timeLable.font = [UIFont fontWithName:@"Georgia" size:10];
        _timeLable.textColor = [UIColor colorFromHexCode:@"#79CDCD"];
        [self.contentView addSubview:_timeLable];
        
        [_clockImagev release];
        [_timeLable release];
        [_footVImagev release];
        [_wayLable release];
        [_touchImageView release];
    }
    return self ;
}



- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    
}

- (void)setWayPointData_wayPoint:(WayPointDataSource_WayPoints *)wayPointData_wayPoint
{
    if (self.wayPointData_wayPoint != wayPointData_wayPoint) {
        [_wayPointData_wayPoint release];
        _wayPointData_wayPoint = [wayPointData_wayPoint retain];
    }
    
    
    [_touchImageView setImageWithURL:[NSURL URLWithString:[_wayPointData_wayPoint.photo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ] placeholderImage:[UIImage imageNamed:@"jiazai1.png"]];
    _wayLable.text = _wayPointData_wayPoint.text;
    _timeLable.text = [self wayPointTime:_wayPointData_wayPoint.date_added];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSString *str = self.wayPointData_wayPoint.text;
    
    NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:12]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    CGFloat imageHeight =[[self.wayPointData_wayPoint.photo_info objectForKey:@"h"] floatValue];
    CGFloat imageWidth =[[self.wayPointData_wayPoint.photo_info objectForKey:@"w"] floatValue];
    if (imageWidth == 0) {
        imageWidth = 1;
    }
    _touchImageView.frame = CGRectMake(0,0, self.contentView.frame.size.width, self.contentView.frame.size.width * imageHeight / imageWidth);
    _wayLable.frame = CGRectMake(0,_touchImageView.frame.size.height, self.contentView.frame.size.width,rect.size.height);
    
    //cell高度 减去 图片 和 文字 的 高度
    CGFloat subImageAndLableHeight =self.contentView.frame.size.height - _touchImageView.frame.size.height - _wayLable.frame.size.height;
    
    //时间lable frame
    _timeLable.frame = CGRectMake(self.contentView.frame.size.width * 6 / 10, _wayLable.frame.size.height + _touchImageView.frame.size.height, self.contentView.frame.size.width * 4 / 10, 20);
    //表图片
    _clockImagev.frame =CGRectMake(self.contentView.frame.size.width * 5 / 10, _wayLable.frame.size.height + _touchImageView.frame.size.height, self.contentView.frame.size.width / 10, 20);
    //横条frame
    _footHImagev.frame = CGRectMake(0, _wayLable.frame.size.height + _touchImageView.frame.size.height + 20, self.contentView.frame.size.width, 5);
    
    //竖条frame
    _footVImagev.frame = CGRectMake(20, _wayLable.frame.size.height + _touchImageView.frame.size.height + 25, 5, subImageAndLableHeight - 25);
    
    
}

#pragma mark 图片点击事件
- (void)imageTouchAction:(TouchImageView *)imageV
{
    NSLog(@"点了一下图片");
    self.block();
}

#pragma mark 时间戳计算

- (NSString *)wayPointTime : (NSNumber *)numberTime
{
    NSTimeInterval timeTV = [numberTime doubleValue];
    
    //    NSString* timeStr = @"2011-01-26 17:40:50";
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
    //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
    //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    //    NSDate* date = [formatter dateFromString:timeStr]; //------------将字符串按formatter转成nsdate
    //    NSLog(@"data === %@",date);
    
    //    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    //    NSLog(@"datanow === %@",datenow);
    //    NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成nsstring
    
    //    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    //    NSLog(@"timeSp(时间戳):%@",timeSp); //时间戳的值
    //    时间戳转时间的方法
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeTV];
    //    NSLog(@"(国际时区的时间)1415107016  = %@",confromTimesp);
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    //    NSLog(@"(当前的时间)confromTimespStr =  %@",confromTimespStr);
    
    return confromTimespStr;
}
@end
